package com.taptech.doufu.ui.adapter

import android.content.Context
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.taptech.doufu.R
import com.taptech.doufu.ui.activity.me.MyCoinActivity
import kotlinx.android.synthetic.main.adapter_coin_option_layout.view.*

class CoinOptionAdapter(context:Context,val firstRecharge:Int): BaseRecyclerAdappter<MyCoinActivity.CoinOption,CoinOptionAdapter.ViewHolder>(context){



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.adapter_coin_option_layout,null)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val item = dataList[position]
        if (firstRecharge == 0) {
            if (item.read_ticket > 0) {
                val ss = SpannableString("${item.coin}豆币+${item.read_ticket}张阅读券")
                val span = ForegroundColorSpan(ContextCompat.getColor(mContext,R.color.text_color_57))
                val start = item.coin.toString().length+3
                val end = start+item.read_ticket.toString().length
                ss.setSpan(span,start,end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
                holder.itemView.tvCoin.text = ss
            }else{
                holder.itemView.tvCoin.text = "${item.coin}豆币"
            }
            holder.itemView.layFirstRecharge.visibility = View.GONE
        }else{
            holder.itemView.tvCoin.text = "${item.coin}豆币"
            if (item.read_ticket > 0) {
                holder.itemView.layFirstRecharge.visibility = View.VISIBLE
                holder.itemView.tvFirstRecharge.text = "首充赠送${item.read_ticket}张阅读券"
            }else{
                holder.itemView.layFirstRecharge.visibility = View.GONE
            }
        }

        holder.itemView.tvMoney.text = "${item.amount/100}元"
        if (item.select){
            holder.itemView.ivSelect.visibility = View.VISIBLE
            holder.itemView.layItem.setBackgroundResource(R.drawable.bg_1dp_fe6e6e_radius4)
        }else{
            holder.itemView.ivSelect.visibility = View.GONE
            holder.itemView.layItem.setBackgroundResource(R.drawable.bg_1dp_e8e8e8_radius4)
        }

    }

    class ViewHolder(view: View): RecyclerView.ViewHolder(view)
}